﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IFClientDLL.Calc;
using HCY.CTP.DBShangZheng;
using IFServerDLL.Model;

namespace IFClientDLL.MyCalc.CalcCommand
{
    
    public abstract class CalcCommand_Base:CalcBase, ICalcCommandVolume, ICalcBase
    {
        public CalcCommand_Base(ITryIF tryif):base(tryif)
        {
        }


        private Guid _LastPlusCalcID;
        private Guid _LastPlusTradeID;
        protected virtual int PlusTradeVolume(ICalc calc, MyIF myif, TradeOnHand trade)
        {
            return 0;
        }
        public void PlusTrade(ICalc calc, MyIF myif, TradeOnHand trade)
        {
            if (calc.CalcID == _LastCloseCalcID && trade.OnhandID == _lastTradeOnhandID)
            {
                return;
            }
            
            MyEnumDirection direction = trade.BuyOrSell;
            var x = new UserOrdBase() { BuyOrSell = direction, CalcID = calc.CalcID, Instrument = myif.Instrument, OpenOrClose = MyEnumOpenClose.Open, Price = myif.GetPrice(direction, 1), UserCreateOrdTime = DateTime.Now, Volume = PlusTradeVolume(calc,myif,trade) };
            if (x.Volume > 0)
            {
                if (_tryif.Calc.IsAutoTrade)
                {
                    _LastPlusCalcID = calc.CalcID;
                    _LastPlusTradeID = trade.OnhandID;
                    _tryif.AddOrder(x);
                    myif.AddCalcMsg(new CalcMsg(this, "Close volume " + x.Volume));
                }
            }

        }
        private Guid _LastOpenNewCalcID;
        private MyEnumDirection _LastOpenDirection;
        protected virtual int OpenNewVolume(ICalc calc, MyIF myif, MyEnumDirection direction)
        {
            return 0;
        }
        public void OpenNew(ICalc calc, MyIF myif, MyEnumDirection direction)
        {
            if (calc.CalcID == _LastOpenNewCalcID && direction == _LastOpenDirection)
            {
                return;
            }

            var x = new UserOrdBase() { BuyOrSell = direction, CalcID = calc.CalcID, Instrument = myif.Instrument, OpenOrClose = MyEnumOpenClose.Open, Price = myif.GetPrice(direction, 1), UserCreateOrdTime = DateTime.Now, Volume = OpenNewVolume(calc, myif, direction) };
            if (x.Volume > 0)
            {
                if (_tryif.Calc.IsAutoTrade)
                {
                    _LastOpenDirection = direction;
                    _LastOpenNewCalcID = calc.CalcID;
                    _tryif.AddOrder(x);

                    myif.AddCalcMsg(new CalcMsg(this, "OpenNew volume " + 1));
                }
            }

        }
        private Guid _LastCloseCalcID;
        private Guid _lastTradeOnhandID;
        protected virtual int CloseTradeVolume(ICalc calc, MyIF myif, TradeOnHand trade)
        {
            return 0;
        }
        public void CloseTrade(ICalc calc, MyIF myif, TradeOnHand trade)
        {

            if (calc.CalcID == _LastCloseCalcID && trade.OnhandID == _lastTradeOnhandID)
            {
                return;
            }
            MyEnumDirection direction = trade.BuyOrSell == MyEnumDirection.Buy ? MyEnumDirection.Sell : MyEnumDirection.Buy;
            var x = new UserOrdBase() { BuyOrSell = direction, CalcID = calc.CalcID, Instrument = myif.Instrument, OpenOrClose = MyEnumOpenClose.CloseToday, Price = myif.GetPrice(direction, 1), UserCreateOrdTime = DateTime.Now, Volume = CloseTradeVolume(calc,myif,trade) };
            if (x.Volume > 0)
            {
                if (_tryif.Calc.IsAutoTrade)
                {
                    _LastCloseCalcID = calc.CalcID;
                    _lastTradeOnhandID = trade.OnhandID;

                    _tryif.AddOrder(x);
                    myif.AddCalcMsg(new CalcMsg(this, "Close volume " + trade.SumALLVolume));
                }
            }

        }
        private Guid _lastCancelCalcID;
        private int _lastOrderbaseSeqNo;
        public void CancelOrder(ICalc calc, MyIF myif, OrderBase order)
        {
            if (calc.CalcID == _lastCancelCalcID && _lastOrderbaseSeqNo == order.SeqNo)
            {
                return;
            }
            if (_tryif.Calc.IsAutoTrade)
            {
                _lastCancelCalcID = calc.CalcID;
                _lastOrderbaseSeqNo = order.SeqNo;
                _tryif.CancelOrder(order.OrdID, calc.CalcID);
            }

        }
       



        public override MyCalcType CalcType
        {
            get { return MyCalcType.Command; }
        }




        
    }
}
